<?php
/** class.admin.php
 * 
 * Creates Admin interface pages
 * 
 * @author Ted, tass2001-at-gmail-dot-com
 * @package phpsimpleblog
 * @version $Id$
 * @website https://code.google.com/p/phpsimpleblog/
 */
class Admin extends PageFactory{
    public function __contstruct(){
        if(!AUTH)
            die(header('Location: /'));
    }
    
    public function commentApprover(){
        $pending = false;
        $this->appendNode($this->importFromText("<div id='comments'></div>"));
        $query = $this->db->query(sprintf("SELECT ID,body as comment_body,name as comment_name,DATE_FORMAT(date,'%%b %%d, %%Y %%H:%%i') as comment_date
                         FROM %s WHERE approved='0'",COMMENT_TABLE));
        while($comment = $query->fetch_assoc()){
            $pending = true;
            $comment_approval = $this->importFromTemplate('comment','layout',$replace_array=$comment);
            $comment_opts = $this->importFromText(sprintf("<span><a href='#' onclick='return handleComment(%s,true,this);'>Approve</a> / <a href='#' onclick='return handleComment(%s,false,this);'>Deny</a></span>",$comment['ID'],$comment['ID']));
            $comment_approval->appendChild($comment_opts);
            $this->appendNode($comment_approval,'comments');
        }
        if(!$pending){
            $this->appendNode($this->importFromText("<div class='message'>There are not comments pending approval</div>"),'comments');
        }
        return $this->printPage();
    }
    
    public function commentManage($appove,$id){
        $cmnt_id = $this->esc($id);
        $query = ($appove) ? sprintf("UPDATE %s SET approved='1' WHERE ID='%s'",COMMENT_TABLE,$cmnt_id) : sprintf("DELETE FROM %s WHERE ID='%s'",COMMENT_TABLE,$cmnt_id);
        $this->db->query($query);
        return json_encode(array('success'=>true));
    }
    
    public function main(){
        return $this->appendNode($this->importFromTemplate('admin','layout',$replace_array=$comment),'comments');
    }
}
?>